package com.vuefile.service.impl;

import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.vuefile.service.LanShenFileService;
import com.vuefile.tool.HttpServletTool;
import com.vuefile.vo.LanShenFileVo;

/**
 * 描述: 文件上传下载
 * 创建人: 赵兴炎  
 * 日期: 2019年8月21日
 */
@Service
public class LanShenFileServiceImpl implements LanShenFileService {

	@Autowired
	private FastFileStorageClient fastFileStorageClient;

	/**
	 * 描述: 上传文件
	 * 创建人: 赵兴炎  
	 * 日期: 2019年8月27日
	 */
	@Override
	public LanShenFileVo upload(MultipartFile file) {
		LanShenFileVo lanShenFileVo = new LanShenFileVo();
		lanShenFileVo.setFileName(file.getOriginalFilename());
		try {
			InputStream inputStream = file.getInputStream();
			long size = file.getSize();
			String extension = FilenameUtils.getExtension(file.getOriginalFilename());
			StorePath storePath = fastFileStorageClient.uploadFile(inputStream, size, extension, null);
			lanShenFileVo.setFileUrl(storePath.getFullPath());
		} catch (Exception e) {
		}
		return lanShenFileVo;
	}

	/**
	 * 描述: 下载文件
	 * 创建人: 赵兴炎  
	 * 日期: 2019年8月27日
	 */
	@Override
	public void downLoad(String fileUrl, String fileName) {
		HttpServletResponse response = HttpServletTool.getResponse();
		ServletOutputStream outputStream = null;
		try {
			StorePath parseFromUrl = StorePath.parseFromUrl(fileUrl);
			String group = parseFromUrl.getGroup();
			String path = parseFromUrl.getPath();
			// 获取文件
			byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
			// 设置相应类型application/octet-stream （注：applicatoin/octet-stream
			// 为通用，一些其它的类型苹果浏览器下载内容可能为空）
			response.reset();
			response.setContentType("applicatoin/octet-stream");
			// 设置头信息 Content-Disposition为属性名 附件形式打开下载文件 指定名称 为 设定的fileName
			response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
			response.setHeader("filename",URLEncoder.encode(fileName, "UTF-8"));
			// 写入到流
			outputStream = response.getOutputStream();
			outputStream.write(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 描述: 打包下载文件
	 * 创建人: 赵兴炎  
	 * 日期: 2019年8月27日
	 */
	@Override
	public void downLoadZip(List<String> filePaths,List<String> fileNames, String zipName) {
		if(filePaths!=null && filePaths.size() > 0) {
			ServletOutputStream outputStream = null;
			ZipOutputStream zos= null;
			try {
				HttpServletResponse response = HttpServletTool.getResponse();
				outputStream = response.getOutputStream();
				zos=new ZipOutputStream(outputStream);
				// 设置头信息 Content-Disposition为属性名 附件形式打开下载文件 指定名称 为 设定的fileName
				response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipName, "UTF-8"));
				for (int i = 0; i < filePaths.size(); i++) {	
					 String filePath = filePaths.get(i);
					 String fileName = fileNames.get(i);
					 StorePath parseFromUrl = StorePath.parseFromUrl(filePath);
					 String group = parseFromUrl.getGroup();
					 String path = parseFromUrl.getPath();
					 byte[] bytes = fastFileStorageClient.downloadFile(group, path, new DownloadByteArray());
					 zos.putNextEntry(new ZipEntry(fileName));
					 zos.write(bytes, 0, bytes.length);
				}	
				zos.closeEntry();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				try {
					if(zos!=null) zos.close();
					if(outputStream!=null) outputStream.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 描述: 删除文件
	 * 创建人: 赵兴炎  
	 * 日期: 2019年8月27日
	 */
	@Override
	public Boolean deleteFile(String fileUrl) {
		StorePath parseFromUrl = StorePath.parseFromUrl(fileUrl);
		String group = parseFromUrl.getGroup();
		String path = parseFromUrl.getPath();
		fastFileStorageClient.deleteFile(group, path);
		return true;
	}

}
